package com.xzsoft.gdwz.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hamcrest.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import xz.facade.user.model.Employee;
import xz.facade.user.service.EmpFacade;


@Controller
@RequestMapping("/emp")
public class EmployeeController {
	
	@Autowired
	private EmpFacade empFacade;
	
	private static final Logger logger = LoggerFactory .getLogger(EmployeeController.class); 
	
	@RequestMapping(value = "/empList",method={RequestMethod.POST})
	public ModelAndView getEmpList(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String empCode = request.getParameter("empCode")==null?"":request.getParameter("empCode");
		String empName = request.getParameter("empName")==null?"":request.getParameter("empName");
		String empSex = request.getParameter("empSex")==null?"":request.getParameter("empSex");
		int start = rows*(page-1);
		int end = page*rows;
		logger.info("start:"+start+"  end:"+end);
		Map params = new HashMap();
		params.put("start", start);
		params.put("end", end);
		params.put("empCode", empCode.trim());
		params.put("empName", empName.trim());
		params.put("empSex", empSex.trim());
		logger.info("*****"+params.toString());
		List list = empFacade.getEmpByConditon(params);		
		//获取总数据条数
		Integer total = empFacade.getEmpTotal(params);		
		Map map = new HashMap();
		map.put("total",total);
		map.put("rows", list);
		net.sf.json.JSONObject jsonArray = net.sf.json.JSONObject.fromObject(map);
		response.getWriter().write(jsonArray.toString());
		logger.info(jsonArray.toString());
		return null;
	}
	
	@RequestMapping(value = "/saveEmp",method={RequestMethod.POST})
	public ModelAndView saveEmp(Employee emp, HttpServletResponse response) throws IOException{
		String empId = emp.getEmpId();
		Map map = new HashMap();
		try {
			if(empId==null||"".equals(empId)){
				String uuid = UUID.randomUUID().toString();
				emp.setEmpId(uuid);
				empFacade.insertEmp(emp);
			}else{
				empFacade.updateEmp(emp);
			}
			map.put("flag", 1);
			map.put("empId", emp.getEmpId());
		} catch (Exception e) {
			map.put("flag", 0);
			map.put("errorMsg",e.getMessage());
			e.printStackTrace();
		}
		net.sf.json.JSONObject jsonObj = net.sf.json.JSONObject.fromObject(map);
		response.getWriter().write(jsonObj.toString());
		logger.info(jsonObj.toString());
		return null;
	}
	
	@RequestMapping(value = "/deleteEmp",method={RequestMethod.POST})
	public ModelAndView deleteEmp(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String empId = request.getParameter("empId");
		Map map = new HashMap();
		try {
			if(empId!=null&&!"".equals(empId)){
				empFacade.deleteEmp(empId);
			}
			map.put("flag", 1);
			map.put("empId", empId);
		} catch (Exception e) {
			map.put("flag", 0);
			map.put("errorMsg","删除失败");
			e.printStackTrace();
		}
		net.sf.json.JSONObject jsonObj = net.sf.json.JSONObject.fromObject(map);
		response.getWriter().write(jsonObj.toString());
		logger.info(jsonObj.toString());
		return null;
	}
}
